package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    //打印log日志，流程性的日志
    private static final Logger log = LoggerFactory.getLogger(SetmealController.class);

    @Reference
    private UserService userService;

    /**
     * 用户校验，认证
     * @return
     */
    @GetMapping("/getUsername")
    public Result getUsername(){

        //获取用户的认证信息
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        //获取用户登录名
        String username = loginUser.getUsername();

        //返回给前端
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }

    /**
     * 校验用户的用户名和密码，登录成功
     * @return
     */
    @RequestMapping("/loginSuccess")
    public Result loginSuccess(){

        return new Result(true,MessageConstant.LOGIN_SUCCESS);
    }

    /**
     * 校验用户的用户名和密码，登录失败
     * @return
     */
    @RequestMapping("/loginFail")
    public Result loginFail(){

        return new Result(false,"用户名或密码错误，请重新登录！");
    }

}
